package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 老杨
 * @version 1.0
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageResult page(DishPageQueryDTO dishDTO) {
        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishDTO);
       return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.add(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });

            //执行 口味表 的 添加
            dishFlavorMapper.addList(flavors);
        }
    }

    @Override
    public DishDTO getById(Long id) {
        //拿到 菜品信息
        Dish dish = dishMapper.getById(id);
        //拿到 口味表信息
        List<DishFlavor> dishFlavors= dishFlavorMapper.getById(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //修改菜品表
        dishMapper.update(dish);
        //修改口味表
            //1.删除 原有口味
        dishFlavorMapper.delete(dishDTO.getId());
            //2.添加修改后的口味
        if (flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });

            //执行 口味表 的 添加
            dishFlavorMapper.addList(flavors);
        }
    }

    @Override
    public void delete(List<Long> id) {
        //正起售 的菜品 不能删除
        for (Long i :id) {
            Dish dish = dishMapper.getById(i);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //关联 套餐的 不能删除 -- 整个 菜品id集合 中只要有一个 有关联套餐，整个删除就失败
        List<Long> setmealIdBydishId = setmealDishMapper.getSetmealIdBydishId(id);
        if (setmealIdBydishId != null && setmealIdBydishId.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除 对应 菜品
        dishMapper.delete(id);
        //删除 菜品对应的 口味表
        dishFlavorMapper.deletes(id);
    }

    @Override
    public void openOrdisable(Integer status,Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id).build();
        //如果菜品关联了 套餐，且套餐处于起售中，则不可停售该菜品
        if (status == 0){
            List<SetmealDish> setmealBydishId = setmealDishMapper.getSetmealBydishId(id);
            for (SetmealDish setmealDish : setmealBydishId){
                Long setmealId = setmealDish.getSetmealId();
                Setmeal select = setmealMapper.select(setmealId);
                if (select.getStatus() == 1){
                    return;
                }
            }
        }
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> list = dishMapper.list(categoryId);
        return list;
    }

    @Override

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
